package coder.user.controller;

import coder.common.BaseContext;
import coder.common.Result;
import coder.exception.BaseException;
import coder.user.entity.Order;
import coder.user.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("add")
    public Result<String> saveOrder(@ModelAttribute Order order) {
        order.setUserId(BaseContext.getCurrentId());
        orderService.save(order);
        return Result.success("保存成功");
    }

    @PostMapping("update")
    public Result<String> updateOrder(@ModelAttribute Order order) {
        orderService.updateOrder(order);
        return Result.success("更新成功");
    }

    @PostMapping("del")
    public Result<String> delOrder(@RequestParam String id) {
        Order order = orderService.getById(id);
        if(order == null) {
            throw new BaseException("订单不存在");
        }
        order.setIsDelete(1);
        orderService.updateOrder(order);
        return Result.success("删除成功");
    }

    @GetMapping("query")
    public Result<List<Order>> queryOrder() {
        return Result.success(orderService.queryOrder());
    }

    @GetMapping("queryOne")
    public Result<Order> queryOne(String id) {
        return Result.success(orderService.queryOne(id));
    }
}
